package com.demo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.demo.entity.PhysicalPreventionLog;
import com.demo.response.Result;
import com.demo.service.IPhysicalPreventionLogService;
import com.demo.service.IVtePreventiveDoctorAdviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = {"物理治疗类"})
@RequestMapping({"/physicalPreventionLog"})
public class PhysicalPreventionLogController {
    @Autowired
    IPhysicalPreventionLogService iPhysicalPreventionLogService;

    @Autowired
    IVtePreventiveDoctorAdviceService adviceService;

    @ApiOperation(value = "物理床位表", tags = {"物理治疗类", "v20210427更新", "v20210507更新"})
    @ApiImplicitParams({@ApiImplicitParam(name = "wardId", value = "病区映射编码"), @ApiImplicitParam(name = "pageNumber", required = false, value = "当前页，默认 1"), @ApiImplicitParam(name = "pageSize", required = false, value = "页大小，默认 10")})
    @GetMapping({"/patientList"})
    public Result patientList(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber, @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize, @RequestParam(name = "wardId", required = false) Integer wardId) {
        return iPhysicalPreventionLogService.physicalList(pageNumber, pageSize, wardId);
    }

    @ApiOperation(value = "新增物理治疗记录", tags = {"物理治疗记录类", "v20210507更新"})
    @PostMapping({"/addPhysicalLog"})
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "adv", value = "vte诊断序列号vte_preventive_id"), @ApiImplicitParam(name = "pat_id", required = false, value = "his_patient_id")})
    public Result addPhysicalLog(@RequestParam(name = "adv", required = false) Integer adv, @RequestParam(name = "pat_id", required = false) Integer pat_id) throws Exception {
        DateTime startTime = DateUtil.parse(DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
        DateTime endTime = DateUtil.offsetMinute(startTime, 30);
        PhysicalPreventionLog physicalPreventionLog = new PhysicalPreventionLog();
        if (adv != null) {
            physicalPreventionLog.setMacAddress("B0:F8:93:37:14:07");
            physicalPreventionLog.setHisPatientId(pat_id);
            physicalPreventionLog.setVtePreventiveId(adv);
            physicalPreventionLog.setEndTime(endTime);
        } else {
            String msg = "{\t\"code\":\t\"ce0bfd15059b68d67688884d7a3d3e8c\",\t\"ipc_id\":\t\"mx00000001\",\t\"ipc_mac\":\t\"B0:F8:93:37:14:07\",\t\"ipc_model\":\t\"mx\",\t\"pat_id\":\t\"896\",\t\"pat_bed\":\t\"896\",\t\"pat_ward\":\t\"666\",\t\"adv\":\t\"940\",\t\"type\":\t2,\t\"time\":\t\"20210510163307\"}";
            JSONObject messageObject = JSONObject.parseObject(msg);
            physicalPreventionLog.setMacAddress(messageObject.get("ipc_mac").toString());
            physicalPreventionLog.setHisPatientId(Integer.valueOf(Integer.parseInt(messageObject.get("pat_id").toString())));
            physicalPreventionLog.setVtePreventiveId(Integer.valueOf(Integer.parseInt(messageObject.get("adv").toString())));
            physicalPreventionLog.setEndTime(endTime);
        }
        return iPhysicalPreventionLogService.addPhysicalLog(physicalPreventionLog);
    }

    @ApiOperation("查询物理按病区")
    @PostMapping({"/doctorIpcAdviceByWardId"})
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "curPage", value = ""), @ApiImplicitParam(name = "wardId", value = "病区编号", defaultValue = "6666")})
    public Result doctorIpcAdviceByWardId(@RequestParam(name = "curPage") Integer curPage, @RequestParam(name = "wardId") String wardId) throws Exception {
        return adviceService.doctorIpcAdviceByWardId(curPage, 5, wardId);
    }

    @ApiOperation(value = "停止物理治疗记录", tags = {"物理治疗类", "v20210508更新"})
    @PostMapping({"/updatePhysicalLogStop"})
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "adv", value = "病区编号", defaultValue = "6666")})
    public Result updatePhysicalLog(@RequestParam(name = "adv", required = false) Integer adv) throws Exception {
        DateTime dateTime = DateUtil.parse(DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
        if (adv == null) {
            String msg = "{\t\"code\":\t\"ce0bfd15059b68d67688884d7a3d3e8c\",\t\"ipc_id\":\t\"mx00000001\",\t\"ipc_mac\":\t\"B0:F8:93:37:14:07\",\t\"ipc_model\":\t\"mx\",\t\"pat_id\":\t\"896\",\t\"pat_bed\":\t\"896\",\t\"pat_ward\":\t\"666\",\t\"adv\":\t\"940\",\t\"type\":\t5,\t\"status\":\t3,\t\"time\":\t\"20210510163329\"}";
            JSONObject messageObject = JSONObject.parseObject(msg);
            adv = Integer.valueOf(Integer.parseInt(messageObject.get("adv").toString()));
        }
        return iPhysicalPreventionLogService.updatePhysicalLogStop(adv, dateTime);
    }
}
